package com.rankeiot.ai.controller;

import com.rankeiot.ai.domain.Application;

import com.rankeiot.core.Current;
import com.rankeiot.core.util.IDUtil;
import com.rankeiot.core.util.StringUtil;
import lombok.RequiredArgsConstructor;

import com.rankeiot.core.anno.Auth;
import com.rankeiot.core.inner.argumet.QueryPage;
import com.rankeiot.core.inner.argumet.QueryParams;
import com.rankeiot.core.util.CollectionUtil;
import org.sagacity.sqltoy.dao.LightDao;
import org.sagacity.sqltoy.model.Page;
import com.rankeiot.core.data.response.Resp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.rankeiot.ai.AiMenu;
import com.rankeiot.core.sqltoy.Sql;


import jakarta.validation.Valid;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* AI应用
*/
@Tag(name="AI应用")
@Auth(AiMenu.APPLICATION)
@RestController
@RequestMapping("application")
@RequiredArgsConstructor
public class ApplicationController{

    final LightDao dao;

    /**
    * 新增AI应用
    */
    @Operation(summary="新增AI应用")
    @PostMapping("add")
    public Resp add(@Valid @RequestBody Application application){
        application.setCreator(Current.user().getUsername());
        application.setCreateTime(new Date());
        if(StringUtil.isEmpty(application.getAppKey())){
            application.setAppKey(IDUtil.getUUID());
        }
        dao.save(application);

        return Resp.ok();
    }

    /**
    * 删除AI应用
    */
    @Operation(summary="删除AI应用")
    @Transactional
    @PostMapping("delete")
    public Resp delete(List<String> ids){
        // dao.loadAll(applications);
        List<Application> applications = CollectionUtil.map(ids,Application::new);
        dao.deleteAll(applications);
        return Resp.ok();
    }

    /**
    * 更新AI应用
    */
    @Operation(summary="更新AI应用")
    @PostMapping("save")
    public Resp save(@Valid @RequestBody Application application){
        dao.update(application);
        return Resp.ok();
    }

    /**
    *  获取AI应用
    */
    @Operation(summary="获取AI应用详细")
    @Auth(AiMenu.APPLICATION)
    @GetMapping("detail")
    public Resp<Application> detail(String id){
       Application application=dao.load(new Application(id));
       return Resp.of(application);
    }
    /**
    * 获取AI应用列表
    */
    @Operation(summary="列出AI应用")
    @Auth(AiMenu.APPLICATION)
    @PostMapping("list")
    public Resp<Application> list(QueryPage query){
        String sql=Sql.select("id,name,memo,mode,app_key,enable")
                .from(Application.TABLE)
                .optional(q -> q
                    .eq(Application.Fields.name)
                    .eq(Application.Fields.memo)
                ).orderByDesc(Application.Fields.id)
                .toString();
        Page result = dao.findPage(query.page(),sql,query.params(),Application.class);
        return Resp.of(result);
    }

}
